package InterviewsExposed;

import java.util.Hashtable;

public class StringsAndArrays {
	
	public static Character firstNonRepeated(String word){
		
		Hashtable<Character, Object> hashTable = new Hashtable<Character, Object>();
		int i, length;
		Character c;
		Object seenOnce = new Object();
		Object seenTwice = new Object();
		
		length = word.length();
		
		//Fill the hashtable
		for (i = 0; i < length; i++){
			
			c = new Character(word.charAt(i));
			Object object = hashTable.get(c);
			if (object == null)
				hashTable.put(c, seenOnce);
			else if (object == seenOnce)
				hashTable.put(c, seenTwice);
		}
		
		for (i = 0; i < length; i++){
			c = new Character(word.charAt(i));
			if (hashTable.get(c) == seenOnce){
				return c;
			}
		}
		
		return null;
	}
	
	public static String removeChars(String str, String remove){
		
		StringBuilder stringBuild = new StringBuilder();
		int strLength, removeLength, i;
		Hashtable<Character, Object> hashTable = new Hashtable<Character, Object>();
		Character cRemove, cStr;
		removeLength = remove.length();
		strLength = str.length();
		Object removeMe = new Object();
		
		//Create a hash table with the elements to remove
		for (i = 0; i < removeLength; i++){
			cRemove = new Character(remove.charAt(i));
			if (hashTable.get(cRemove)==null){
				hashTable.put(cRemove, removeMe);
			}
		}
		
		//Look into the hash table and create a new string from the characters we want to keep
		for (i = 0; i < strLength; i++){
			cStr = new Character(str.charAt(i));
			if (hashTable.get(cStr) != removeMe){
				stringBuild.append(cStr);
			}
		}
		
		return stringBuild.toString();
	}
	
	public static String reverseSentence(String sentence){
		int start = 0, end  = 0;
		int length = sentence.length();
		char[] seq = reverseWords(sentence.toCharArray(), start, length - 1);
		char[] newSeq = new char[seq.length];
		
		while (end < length){
			
			if (seq[end] != ' '){
				start = end;
				
				while (end < length && seq[end] != ' '){
					end ++;
				}
				end--;
				
				sentence.substring(start, end).toCharArray();
				//Incomplete
			}
		}
		
		return null;
	}
	
	private static char[] reverseWords(char[] charSeq, int start, int end){
		
		while (end > start){
			char temp = charSeq[end];
			charSeq[end] = charSeq[start];
			charSeq[start] = temp;
			end--;
			start++;
		}
		
		return charSeq;
	}
	
	public static int strToInt(String str){
		
		int i = 0, num = 0;
		boolean isNeg = false;
		int length = str.length();
		
		if(str.charAt(i) == '-'){
			isNeg = true;
			i = 1;
		}
		
		while (i < length){
			
			num *= 10;
			num += (str.charAt(i) - '0');
			i++;
		}
		
		if (isNeg){
			num *= -1;
		}
		
		return num;
	}
	
	public static String intToStr(int num){
		
		StringBuilder builder = new StringBuilder();
		boolean isNeg = false;
		char[] temp = new char[10];
		int i = 0;
		
		if (num < 0){
			isNeg = true;
			builder.append('-');
			num *= -1;
		}
		
		while (num != 0){
			temp[i] = (char)(num%10 + '0');
//			System.out.println("El modulo 10 de " + num + " es: "+ num%10);
//			System.out.println("Ahora " + num + " / 10: "+ num/10);
			num /= 10;
			i++;
		}
		
		while (i >= 0){
			builder.append(temp[i--]);
		}
		
		return builder.toString();
	}
}
